package com.wondertek.poms.core.service.impl;

import cn.hutool.core.convert.Convert;
import com.netflix.discovery.converters.Auto;
import com.wondertek.poms.common.utils.StringUtil;
import com.wondertek.poms.dao.po.EquityTree;
import com.wondertek.poms.dao.repo.IEquityNodeDao;
import com.wondertek.poms.dao.repo.IPrdContDao;
import com.wondertek.poms.dao.repo.IPrdInfoDao;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wondertek.poms.common.service.impl.ServiceImpl;
import com.wondertek.poms.core.service.IEquityNodeService;
import com.wondertek.poms.dao.po.EquityNode;
import com.wondertek.poms.dao.po.MamRecommend;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.xml.transform.Templates;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p><b>PcProductRuleServiceImpl Description:</b> 二级产品规则 service层实现 </p>
 *
 * @author 窦再兴
 * <p><b>DATE</b> 2020-10-16 15:56:15</p>
 */
@Service
public class EquityNodeServiceImpl extends ServiceImpl<EquityNode, Long> implements IEquityNodeService {

	@Autowired
	private IEquityNodeDao equityNodeDao;
	@Autowired
	private IPrdInfoDao prdInfoDao;
	@Autowired
	private IPrdContDao prdContDao;
	@PersistenceContext
    public EntityManager entityManager;

	@Override
	public EquityNode deleteLogic(Long id) {
		return null;
	}

	@Override
	public EquityNode merge(EquityNode model) {
		return null;
	}


	@Override
	public List<EquityNode> findForSelect() {
		List<EquityNode> list=new ArrayList<EquityNode>();
			String sql= " select en.ID," +
					"en.EQUITY_TREE_ID,"+
					"en.GROUP_ID,"+
					"en.HIERARCHY,"+
					"en.PARENT_ID,"+
					"en.STATUS,"+
					"en.EQUITY_NODE_ID,"+
					"en.NODE_ALIAS,"+
					"en.TREE_ID,"+
					"en.NAME,"+
					"en.UPDATE_TIME,"+
					"en.SAVE_STATUS,"+
					"en.DELETE_STATUS,"+
					"en.MC_LABEL from Equity_Node en,Equity_Tree et " +
					"where en.id in (select max(enn.id) from Equity_Node enn where  enn.status = '0' group by enn.Equity_Node_Id)" +
					" and en.tree_Id = et.id and et.ACTIVES_STATUS = '0'";
			Query query= entityManager.createNativeQuery(sql);
			List<Object[]> rows= query.getResultList();
			if(rows!=null && rows.size()>0){
				for (Object[] obj:rows) {
					EquityNode equityNode=new EquityNode();
					equityNode.setId(StringUtil.nullToLong(obj[0]));
					equityNode.setTreeId(StringUtil.nullToLong(obj[1]));
					equityNode.setGroupId(StringUtil.nullToLong(obj[2]));
					equityNode.setHierarchy(StringUtil.nullToInteger(obj[3]));
					equityNode.setParentId(StringUtil.nullToLong(obj[4]));
					equityNode.setStatus(StringUtil.nullToString(obj[5]));
					equityNode.setEquityNodeId(StringUtil.nullToString(obj[6]));
					equityNode.setName(StringUtil.nullToString(obj[7]));
					list.add(equityNode);
				}
			}
		return list;
	}

	@Override
	public List<EquityNode> findAllByTreeId(String id) {
		return equityNodeDao.findAllByTreeIdAndDeleteStatus(Convert.toLong(id),0);
	}

	/**
	 * 如果该节点还存在子节点或处于启用状态，则不能删除
	 * @param id
	 * @return
	 */
	@Override
	public String deleteByNodeId(Long id) {
		String msg="";

		EquityNode equityNode = jpaRepository.getOne(id);
		if(equityNode !=null){
			if(equityNode.getStatus().equals("0")){
				msg="该节点处于启用状态，不能删除!";
				return msg;
			}
		}
		equityNode.setDeleteStatus(1);
		equityNodeDao.save(equityNode);
		return msg;
	}

	/**
	 * 检查权益树下节点有没有被配置  false:未被配置，可以删除    true：已被配置，不能删除
	 * @param treeId
	 * @return
	 */
	@Override
	public Boolean checkEquityNodeExist(Long treeId) {
		List<EquityNode> list=equityNodeDao.findAllByTreeIdAndDeleteStatus(treeId,0);
		/*if(list!= null && list.size()==1 && list.get(0).getParentId()==0){
			flag=false;
		}*/
		for(EquityNode equityNode:list){
			if(!"".equals(checkUseOfNode(equityNode.getId()))){
				return true;
			}
		}
		return false;
	}

	/**
	 * 检查节点是否可以禁用
	 * 如果该权益节点在节目或商品包上已配置，则不能禁用
	 * @param id
	 * @return
	 */
	@Override
	public String checkNodeStatus(Long id) {
		String msg="";
		msg=checkUseOfNode(id);
		List<EquityNode> list=equityNodeDao.findAllByParentIdAndDeleteStatus(id,0);
		if(list != null && list.size()>0){
			msg+="该节点存在子节点，不能禁用！ ";
		}
		return msg;
	}

	public String checkUseOfNode(Long id){
		String msg="";
		int num=prdInfoDao.getTotalUseOfEquityid("%" + id + "%");
		if(num > 0){
			msg+="该节点在商品包上已配置，不能禁用！ ";
		}
		int prdContNum=prdContDao.getTotalUseOfEquity("%" + id + "%");
		if(prdContNum > 0){
			msg+="该节点在节目上已配置，不能禁用！ ";
		}
		return msg;
	}

	@Override
	public List<EquityNode> findAllByNodeName(String nodeName) {
		return equityNodeDao.findAllByName(nodeName);
	}

}